<?php

class Formulario {

    public $link;

    function Formulario() {
        
    }

    function combobox($array, $name, $value = 0, $elemetoOne = 'Todos', $evento = "") {
        $salida = "<select name='{$name}' class='normalselect' id='{$name}' {$evento} >";
        $salida .= "<option value=\"0\">" . $elemetoOne . "</option> ";
        foreach ($array as $llave => $valor) {
            $selected = "";
            if ($llave == $value) {
                $selected = "selected='selected'";
            }
            $salida .= "<option value='{$llave}' {$selected}>" . $valor . "</option>";
        }
        $salida .= "</select>";
        return $salida;
    }

    function input($name = "", $value = "", $size = -1, $maxlength = -1, $class = "", $id = "", $evento = "") {
        $salida = '<input type="text" ';

        if ($name <> "") {
            $salida .=' name="' . $name . '" ';
        }
        if ($value <> "") {
            $salida .=' value="' . $value . '" ';
        }
        if ($size > -1) {
            $salida .=' size="' . $size . '" ';
        }
        if ($maxlength > -1) {
            $salida .=' maxlength="' . $maxlength . '" ';
        }
        if ($class <> "") {
            $salida .=' class="' . $class . '" ';
        }
        if ($id <> "") {
            $salida .=' id="' . $id . '" ';
        }
        if ($evento <> "") {
            $salida .=' ' . $evento . ' ';
        }
        $salida .= ' />';
        return $salida;
    }

    function textarea($name = "", $value = "", $cols = -1, $rows = -1, $class = "", $id = "", $evento = "") {
        $salida = '<textarea ';
        if ($name <> "") {
            $salida .=' name="' . $name . '" ';
        }
        if ($cols > -1) {
            $salida .=' cols="' . $cols . '" ';
        }
        if ($rows > -1) {
            $salida .=' rows="' . $rows . '" ';
        }
        if ($class > "") {
            $salida .=' class="' . $class . '" ';
        }
        if ($id <> "") {
            $salida .=' id="' . $id . '" ';
        }
        if ($evento <> "") {
            $salida .=' ' . $evento . '" ';
        }
        $salida .= ' />';
        if ($value <> "") {
            $salida .=$value;
        }
        $salida .= '</textarea>';
        return $salida;
    }

    function checkbox($array, $name, $seleccionados = "", $evento = "") {
        $salida = "";
        $seleccion = "";
        foreach ($array as $llave => $valor) {
            if ($seleccionados != "")
                if ($this->existeOpcion($seleccionados, $llave)) {
                    $seleccion = "checked";
                } else {
                    $seleccion = "";
                }
            $salida .= "<input type=\"checkbox\" name=\"" . $name . "" . $llave . "\" id=\"" . $name . "" . $llave . "\" value=\"" . $llave . "\" " . $seleccion . "  " . $evento . " />&nbsp&nbsp" . $valor . "<br>";
        }
        return $salida;
    }

    function checkboxColumn($array, $name, $seleccionados = "", $column = "", $evento = "") {
        $count = 0;
        $countArray = count($array);
        $resto = ($countArray % $column);
        $columnasAdd = ($column - $resto);

        $salida .= "<table border='0' bodercolor='#FFF' >";
        $salida .= "<tr>";
        foreach ($array as $llave => $valor) {
            if ($count == $column) {
                $salida .= "<tr>";
            }
            $salida .= "<td>";
            if ($seleccionados != "")
                if ($this->existeOpcion($seleccionados, $llave)) {
                    $seleccion = "checked";
                } else {
                    $seleccion = "";
                }
            $salida .= "<input type=\"checkbox\" name=\"" . $name . "" . $llave . "\" id=\"" . $name . "" . $llave . "\" value=\"" . $llave . "\" " . $seleccion . "  " . $evento . " /><label for=\"" . $name . "" . $llave . "\" >" . $valor . "</label>";
            $salida .= "</td>";
            if ($count == $column - 1) {
                $salida .= "</tr>";
                $count = 0;
            } else {
                $count++;
            }
        }

        for ($i = 0; $i < $columnasAdd; $i++) {
            $salida .= "<td>";
            $salida .= "</td>";
        }

        $salida .= "</tr>";
        $salida .= "</table>";
        return $salida;
    }

    function existeOpcion($lista, $opcion) {
        $exist = false;
        $aux_lista = explode(',', $lista);
        for ($i = 0; $i < count($aux_lista); $i++) {
            if ($aux_lista[$i] == $opcion) {
                $exist = true;
                break;
            }
        }
        return $exist;
    }

    function radiobutton($array, $name, $seleccionados = "", $evento = "") {
        $salida = "";
        $seleccion = "";
        foreach ($array as $llave => $valor) {
            if ($seleccionados != "")
                if ($this->existeOpcion($seleccionados, $llave)) {
                    $seleccion = "checked";
                } else {
                    $seleccion = "";
                }
            $salida .= "<input type=\"radio\" name=\"" . $name . "\" id=\"" . $name . "" . $llave . "\" value=\"" . $llave . "\" " . $seleccion . "  " . $evento . " />&nbsp&nbsp" . $valor . "<br>";
        }
        return $salida;
    }
}
?>